package com.pcbly.web.job;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Time {
	public static final long SECOND = 1000;

	public static final long MINUTE = SECOND * 60;

	public static final long HOUR = MINUTE * 60;

	public static final long DAY = HOUR * 24;

	public static final long WEEK = DAY * 7;

	public static final long MONTH = DAY * 30;

	public static final long YEAR = DAY * 365;

	public static Date getDate(Calendar cal) {
		Calendar adjustedCal = new GregorianCalendar();
		adjustedCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		adjustedCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		adjustedCal.set(Calendar.DATE, cal.get(Calendar.DATE));
		adjustedCal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
		adjustedCal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
		adjustedCal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
		adjustedCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND)); 
		return adjustedCal.getTime();
	}

	public static Date getDate(Date date, TimeZone tz) {
		Calendar cal = new GregorianCalendar(tz);
		cal.setTime(date); 
		return getDate(cal);
	}

	public static String getDescription(long milliseconds) {
		String s = "";

		int x = 0;

		if (milliseconds % YEAR == 0) {
			x = (int) (milliseconds / YEAR);

			s = x + " YEAR";
		} else if (milliseconds % MONTH == 0) {
			x = (int) (milliseconds / MONTH);

			s = x + " MONTH";
		} else if (milliseconds % WEEK == 0) {
			x = (int) (milliseconds / WEEK);

			s = x + " Week";
		} else if (milliseconds % DAY == 0) {
			x = (int) (milliseconds / DAY);

			s = x + " Day";
		} else if (milliseconds % HOUR == 0) {
			x = (int) (milliseconds / HOUR);

			s = x + " Hour";
		} else if (milliseconds % MINUTE == 0) {
			x = (int) (milliseconds / MINUTE);

			s = x + " Minute";
		} 

		return s;
	}

}
